import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

import metier.IMetier;
import metier.MetierImpl;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import dao.DaoImpl;
import dao.IDao;

public class Presentation {
	
	public static void modele_1() {
		// Modele instanciation statique
		IDao dao = new DaoImpl();
		MetierImpl metier = new MetierImpl();
		metier.setDao(dao);
		System.out.println(metier.calcule());
	}
	
	public static void modele_2() {
		// Modele IOC : reflexion
		try {
			Scanner scan = new Scanner(new File("WebContent/config.txt"));
			String daoClassName = scan.next();
			String metierClassName = scan.next();
			Class cdao = Class.forName(daoClassName);
			IDao dao = (IDao) cdao.newInstance(); 
			Class cmetier = Class.forName(metierClassName);
			IMetier metier = (IMetier) cmetier.newInstance();
			Method meth = cmetier.getMethod("setDao", new Class[]{IDao.class});
			meth.invoke(metier, new Object[]{dao});
			System.out.println(metier.calcule());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void modele_3() {
		// Spring
		XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("config.xml"));
		IDao dao = (IDao) bf.getBean("objDao");
		IMetier metier = (IMetier) bf.getBean("objMetier");
//		System.out.println(metier.calcule());
	}
	
	public static void main(String[] args) {
		modele_3();
	}
}
